package com.icore.bean;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;

import org.json.JSONException;
import org.json.JSONObject;

import com.icore.AppException;

/**
 * 用户
 * 
 */
public class User extends BaseEntity {

	private int roleID;
	private String roleDes;
	private int userID;
	private String userDes;

	public int getRoleID() {
		return roleID;
	}

	public void setRoleID(int roleID) {
		this.roleID = roleID;
	}

	public String getRoleDes() {
		return roleDes;
	}

	public void setRoleDes(String roleDes) {
		this.roleDes = roleDes;
	}

	public int getUserID() {
		return userID;
	}

	public void setUserID(int userID) {
		this.userID = userID;
	}

	public String getUserDes() {
		return userDes;
	}

	public void setUserDes(String userDes) {
		this.userDes = userDes;
	}

	public static User parse(InputStream stream) throws AppException {
		User user = new User();

		try {
			String res = readByByte(stream, -1, new ByteArrayOutputStream(1024));
			if (res != null && res.length() > 0) {
				try {
					JSONObject resObj = new JSONObject(res);
					int code = resObj.getInt("code");
					user.setResCode(code);
					// 失败:{code:0,description:数据库语句异常,认证失败}
					if (code == 0) {
						user.setResMessage(resObj.getString("description"));
					}
					// 成功:{"data":{"user_description":"李铭晟","role_id":"4","user_id":"1","role_descript":"游客"},"descript":"认证成功","code":1}
					else if (code == 1) {
						user.setResMessage(resObj.getString("description"));

						String dataStr = resObj.getString("data");
						JSONObject dataObj = new JSONObject(dataStr);
						user.setRoleID(dataObj.getInt("role_id"));
						user.setRoleDes(dataObj.getString("role_descript"));
						user.setUserID(dataObj.getInt("user_id"));
						user.setUserDes(dataObj.getString("user_description"));
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
		} catch (AppException e) {
			e.printStackTrace();
			throw AppException.io(e);
		}
		return user;
	}

}
